
/*
 * ScreenStack.java
 * Created on August 9, 2008, 1:02 PM by Uru
 *
 * A static class that contains the game screens
 */
package ScreenSystem;

import java.util.*;
import LuaSystem.LuaEngine;
import org.keplerproject.luajava.*;

public class ScreenStack {
    protected static LinkedList<Screen> stack;              //the stack itself
    protected static Hashtable<String,ScreenData> nametable;//a table of menu names mapped either to the Screen itself or a filename
    
    //initialize the stack with the first screen
    public static void initializeStack(){
        stack = new LinkedList<Screen>();
        nametable = new Hashtable<String,ScreenData>();
        initializeLuaFunctions();
    }
    
    //initialize the stack with the first screen
    public static void initializeStack(Screen firstscreen){
        stack = new LinkedList<Screen>();
        stack.push(firstscreen);
        nametable = new Hashtable<String,ScreenData>();
        initializeLuaFunctions();
    }
    
    //register lua functions
    private static void initializeLuaFunctions(){
        LuaState lua = LuaEngine.getLua();
        
        //register pushScreen(name)
        PushScreen pushscreen = new PushScreen(lua);
        try{
            pushscreen.register("pushScreen");
        }catch(LuaException e){
            System.err.println("Failed to register pushScreen() in Lua");
            System.err.println(e.toString());
        }
        //register popScreen()
        PopScreen popscreen = new PopScreen(lua);
        try{
            popscreen.register("popScreen");
        }catch(LuaException e){
            System.err.println("Failed to register popScreen() in Lua");
            System.err.println(e.toString());
        }
        //register replaceScreen(name)
        ReplaceScreen replacescreen = new ReplaceScreen(lua);
        try{
            replacescreen.register("replaceScreen");
        }catch(LuaException e){
            System.err.println("Failed to register replaceScreen() in Lua");
            System.err.println(e.toString());
        }
    }
    
    //push a new screen onto the stack
    public static void push(Screen newscreen){
        stack.push(newscreen);
    }
    
    //pop the top screen off of the stack
    public static void pop(){
        stack.pop();
    }
    
    //replace the top screen with another one
    public static void replace(Screen replacement){
        stack.pop();
        stack.push(replacement);
    }
    
    //get the top screen
    public static Screen getTopScreen(){
        //JSR: Quick Edit, peek wasn't working.        
        //return stack.peek();
        return stack.getFirst();
    }
    
    //get the list, most likely to iterate through it
    public static LinkedList<Screen> getStack(){
        return stack;
    }
    
    //register a screen with the data table
    public static void registerScreen(String name, String filename){
        nametable.put(name,new ScreenData(name,filename));
    }
    
    //register a screen with the data table
    public static void registerScreen(String name, Screen sc){
        nametable.put(name,new ScreenData(name,sc));
    }
    
    //get a screen from the table
    public static Screen getScreen(String name){
        ScreenData d = nametable.get(name);
        if(d != null){
            return d.loadScreen();
        }
        return null;
    }
}
